<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveSupport::LogSubscriber</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Class</span> 
            ActiveSupport::LogSubscriber 
            
                <span class="parent">&lt; 
                    
                    Subscriber
                    
                </span>
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/activesupport/lib/active_support/log_subscriber_rb.html">activesupport/lib/active_support/log_subscriber.rb</a></li>
            
            <li><a href="../../files/activesupport/lib/active_support/log_subscriber/test_helper_rb.html">activesupport/lib/active_support/log_subscriber/test_helper.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p><a href="LogSubscriber.html">ActiveSupport::LogSubscriber</a> is an object
set to consume <a
href="Notifications.html">ActiveSupport::Notifications</a> with the sole
purpose of logging them. The log subscriber dispatches notifications to a
registered object based on its given namespace.</p>

<p>An example would be Active Record log subscriber responsible for logging
queries:</p>

<pre class="ruby"><span class="ruby-keyword">module</span> <span class="ruby-constant">ActiveRecord</span>
  <span class="ruby-keyword">class</span> <span class="ruby-constant">LogSubscriber</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">LogSubscriber</span>
    <span class="ruby-keyword">def</span> <span class="ruby-identifier">sql</span>(<span class="ruby-identifier">event</span>)
      <span class="ruby-node">&quot;#{event.payload[:name]} (#{event.duration}) #{event.payload[:sql]}&quot;</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>And it’s finally registered as:</p>

<pre>ActiveRecord::LogSubscriber.attach_to :active_record</pre>

<p>Since we need to know all instance methods before attaching the log
subscriber, the line above should be called after your
<code>ActiveRecord::LogSubscriber</code> definition.</p>

<p>After configured, whenever a “sql.active_record” notification is published,
it will properly dispatch the event (<a
href="Notifications/Event.html">ActiveSupport::Notifications::Event</a>) to
the sql method.</p>

<p>Log subscriber also has some helpers to deal with logging and automatically
flushes all logs when the request finishes (via action_dispatch.callback
notification) in a <a href="../Rails.html">Rails</a> environment.</p>

    </div>
  


  


  
  


  
    <!-- Namespace -->
    <div class="sectiontitle">Namespace</div>
    <ul>
      
        <li>
          <span class="type">MODULE</span>
          <a href="LogSubscriber/TestHelper.html">ActiveSupport::LogSubscriber::TestHelper</a>
        </li>
      
    </ul>
  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>C</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="LogSubscriber.html#method-i-color">color</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>F</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="LogSubscriber.html#method-i-finish">finish</a>,
              </li>
            
              
              <li>
                <a href="LogSubscriber.html#method-c-flush_all-21">flush_all!</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>L</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="LogSubscriber.html#method-c-log_subscribers">log_subscribers</a>,
              </li>
            
              
              <li>
                <a href="LogSubscriber.html#method-i-logger">logger</a>,
              </li>
            
              
              <li>
                <a href="LogSubscriber.html#method-c-logger">logger</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>S</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="LogSubscriber.html#method-i-start">start</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    
      <!-- Section constants -->
      <div class="sectiontitle">Constants</div>
      <table border='0' cellpadding='5'>
        
          <tr valign='top'>
            <td class="attr-name">CLEAR</td>
            <td>=</td>
            <td class="attr-value">&quot;\e[0m&quot;</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"><p>Embed in a <a href="../String.html">String</a> to clear all previous ANSI
sequences.</p></td>
            </tr>
          
        
          <tr valign='top'>
            <td class="attr-name">BOLD</td>
            <td>=</td>
            <td class="attr-value">&quot;\e[1m&quot;</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
          <tr valign='top'>
            <td class="attr-name">BLACK</td>
            <td>=</td>
            <td class="attr-value">&quot;\e[30m&quot;</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"><p>Colors</p></td>
            </tr>
          
        
          <tr valign='top'>
            <td class="attr-name">RED</td>
            <td>=</td>
            <td class="attr-value">&quot;\e[31m&quot;</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
          <tr valign='top'>
            <td class="attr-name">GREEN</td>
            <td>=</td>
            <td class="attr-value">&quot;\e[32m&quot;</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
          <tr valign='top'>
            <td class="attr-name">YELLOW</td>
            <td>=</td>
            <td class="attr-value">&quot;\e[33m&quot;</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
          <tr valign='top'>
            <td class="attr-name">BLUE</td>
            <td>=</td>
            <td class="attr-value">&quot;\e[34m&quot;</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
          <tr valign='top'>
            <td class="attr-name">MAGENTA</td>
            <td>=</td>
            <td class="attr-value">&quot;\e[35m&quot;</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
          <tr valign='top'>
            <td class="attr-name">CYAN</td>
            <td>=</td>
            <td class="attr-value">&quot;\e[36m&quot;</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
          <tr valign='top'>
            <td class="attr-name">WHITE</td>
            <td>=</td>
            <td class="attr-value">&quot;\e[37m&quot;</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
      </table>
    


    
      <!-- Section attributes -->
      <div class="sectiontitle">Attributes</div>
      <table border='0' cellpadding='5'>
        
          <tr valign='top'>
            <td class='attr-rw'>
              [W]
            </td>
            <td class='attr-name'>logger</td>
            <td class='attr-desc'></td>
          </tr>
        
      </table>
    


    <!-- Methods -->
    
      <div class="sectiontitle">Class Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-c-flush_all-21">
            
              <b>flush_all!</b>()
            
            <a href="LogSubscriber.html#method-c-flush_all-21" name="method-c-flush_all-21" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Flush all <a
href="LogSubscriber.html#method-c-log_subscribers">::log_subscribers</a>’
logger.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-flush_all-21_source')" id="l_method-c-flush_all-21_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/5f07048dfdcc86bf78d0a498c5f23fa4dec917f7/activesupport/lib/active_support/log_subscriber.rb#L69" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-flush_all-21_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/log_subscriber.rb, line 69</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">flush_all!</span>
  <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">flush</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:flush</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-c-log_subscribers">
            
              <b>log_subscribers</b>()
            
            <a href="LogSubscriber.html#method-c-log_subscribers" name="method-c-log_subscribers" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-log_subscribers_source')" id="l_method-c-log_subscribers_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/5f07048dfdcc86bf78d0a498c5f23fa4dec917f7/activesupport/lib/active_support/log_subscriber.rb#L64" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-log_subscribers_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/log_subscriber.rb, line 64</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">log_subscribers</span>
  <span class="ruby-identifier">subscribers</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-c-logger">
            
              <b>logger</b>()
            
            <a href="LogSubscriber.html#method-c-logger" name="method-c-logger" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-logger_source')" id="l_method-c-logger_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/5f07048dfdcc86bf78d0a498c5f23fa4dec917f7/activesupport/lib/active_support/log_subscriber.rb#L56" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-logger_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/log_subscriber.rb, line 56</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">logger</span>
  <span class="ruby-ivar">@logger</span> <span class="ruby-operator">||=</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">defined?</span>(<span class="ruby-constant">Rails</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-constant">Rails</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:logger</span>)
    <span class="ruby-constant">Rails</span>.<span class="ruby-identifier">logger</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                  
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-finish">
            
              <b>finish</b>(name, id, payload)
            
            <a href="LogSubscriber.html#method-i-finish" name="method-i-finish" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-finish_source')" id="l_method-i-finish_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/5f07048dfdcc86bf78d0a498c5f23fa4dec917f7/activesupport/lib/active_support/log_subscriber.rb#L82" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-finish_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/log_subscriber.rb, line 82</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">finish</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">id</span>, <span class="ruby-identifier">payload</span>)
  <span class="ruby-keyword">super</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">logger</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
  <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">error</span> <span class="ruby-node">&quot;Could not log #{name.inspect} event. #{e.class}: #{e.message} #{e.backtrace}&quot;</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-logger">
            
              <b>logger</b>()
            
            <a href="LogSubscriber.html#method-i-logger" name="method-i-logger" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-logger_source')" id="l_method-i-logger_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/5f07048dfdcc86bf78d0a498c5f23fa4dec917f7/activesupport/lib/active_support/log_subscriber.rb#L74" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-logger_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/log_subscriber.rb, line 74</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">logger</span>
  <span class="ruby-constant">LogSubscriber</span>.<span class="ruby-identifier">logger</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-start">
            
              <b>start</b>(name, id, payload)
            
            <a href="LogSubscriber.html#method-i-start" name="method-i-start" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-start_source')" id="l_method-i-start_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/5f07048dfdcc86bf78d0a498c5f23fa4dec917f7/activesupport/lib/active_support/log_subscriber.rb#L78" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-start_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/log_subscriber.rb, line 78</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">start</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">id</span>, <span class="ruby-identifier">payload</span>)
  <span class="ruby-keyword">super</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">logger</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
              
      <div class="sectiontitle">Instance Protected methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-color">
            
              <b>color</b>(text, color, bold=false)
            
            <a href="LogSubscriber.html#method-i-color" name="method-i-color" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Set color by using a string or one of the defined constants. If a third
option is set to <code>true</code>, it also adds bold to the string. This
is based on the Highline implementation and will automatically append <a
href="LogSubscriber.html#CLEAR">CLEAR</a> to the end of the returned <a
href="../String.html">String</a>.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-color_source')" id="l_method-i-color_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/5f07048dfdcc86bf78d0a498c5f23fa4dec917f7/activesupport/lib/active_support/log_subscriber.rb#L102" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-color_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/log_subscriber.rb, line 102</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">color</span>(<span class="ruby-identifier">text</span>, <span class="ruby-identifier">color</span>, <span class="ruby-identifier">bold</span>=<span class="ruby-keyword">false</span>)
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">text</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">colorize_logging</span>
  <span class="ruby-identifier">color</span> = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">const_get</span>(<span class="ruby-identifier">color</span>.<span class="ruby-identifier">upcase</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">color</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>)
  <span class="ruby-identifier">bold</span>  = <span class="ruby-identifier">bold</span> <span class="ruby-operator">?</span> <span class="ruby-constant">BOLD</span> <span class="ruby-operator">:</span> <span class="ruby-string">&quot;&quot;</span>
  <span class="ruby-node">&quot;#{bold}#{color}#{text}#{CLEAR}&quot;</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    